home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS18.ADF / Progs / SetPrefs / SetPrefs.c < prev    next >
C/C++ Source or Header  |  1989-01-27  |  5KB  |  187 lines

  1. /** SetPrefs.c
  2. *
  3. *   SetPrefs, Copyright (C) 1987 by W.G.J. Langeveld
  4. *
  5. *   Description:
  6. *   ------------
  7. *       Have you ever had the problem that you reassigned all your system 
  8. *       directories to your hard disk or to another floppy or to your re-
  9. *       coverable ram disk, and at some point changed and saved your Pre-
  10. *       ferences, but to the wrong place?   So that when you have to boot
  11. *       you wind up with  the same old preferences that AmigaDOS picks up
  12. *       from the system-configuration file on the floppy you boot from?
  13. *           If so, this is a program for you. You run it in your startup-
  14. *       sequence AFTER you have reassigned especially devs: to your other
  15. *       device. This program will then read the system-configuration file
  16. *       in the new devs: directory and set your default preferences accor-
  17. *       dingly.
  18. *           Moreover,  if you just want to use  the preferences from some
  19. *       other disk or  file, you can  specify it on  the command line: in 
  20. *       that case the  program will  search for the specified file rather
  21. *       than 'devs:system-configuration'.
  22. *
  23. *   Examples of use:
  24. *   ----------------
  25. *   (1) A sample startup-sequence might look like:
  26. *
  27. *       assign C: hd0:c
  28. *       assign S: hd0:s
  29. *       assign L: hd0:l
  30. *       assign FONTS: hd0:fonts
  31. *       assign DEVS: hd0:devs
  32. *       assign LIBS: hd0:libs
  33. *       assign SYS: hd0:
  34. *
  35. *       echo "Everything reassigned"
  36. *
  37. *       SetPrefs
  38. *
  39. *   (2) Suppose I  would like to  have the colors I get when booting from
  40. *       my DrofNats program disk, but don't want to boot from it. I could
  41. *       either get into Preferences  and set the colors from memory, or I
  42. *       could type from the CLI:
  43. *
  44. *       1> SetPrefs DrofNats:devs/system-configuration
  45. *
  46. *       If the DrofNats disk  is not in a drive, AmigaDOS will prompt you
  47. *       to insert it.
  48. *
  49. *   (3) Suppose I have a hard disk (I don't)  but would like to change the
  50. *       set-up on occasion,   because e.g.  sometimes I  use  a  different
  51. *       monitor than other times. One might have several system-configura-
  52. *       tion files stashed  away someplace,  and change to any one of them 
  53. *       by typing from the CLI:
  54. *
  55. *       1> SetPrefs :configfiles/config25
  56. *
  57. *       You can make config files by going into preferences, saving them
  58. *       and copying the resulting system-configuration file (Preferences
  59. *       always writes to devs:system-configuration):
  60. *
  61. *       1> copy devs:system-configuration :configfiles/config25
  62. *
  63. *
  64. *   Have fun!
  65. *               Willy.
  66. *
  67. **/
  68. #include "exec/exec.h"
  69. #include "intuition/intuition.h"
  70. #include "libraries/dos.h"
  71. #include "functions.h"
  72. #include "stdio.h"
  73.  
  74. struct IntuitionBase *IntuitionBase = NULL;
  75. struct Preferences *prefs = NULL, *nprefs = NULL;
  76.  
  77. FILE *fp = NULL;
  78.  
  79. char sysconf[] = "devs:system-configuration";
  80.  
  81. long dos_lenfil();
  82.  
  83. #define PSIZE ((long) sizeof(struct Preferences))
  84.  
  85. main(argc, argv)
  86. int argc;
  87. char **argv;
  88. {
  89.    int nread;
  90.    long length;
  91.    char *conffil;
  92.  
  93.    if (argc > 2) cleanup("Usage: %s [file-name]\n", argv[0]);
  94. /*
  95. *  Get Intuition
  96. */
  97.    IntuitionBase = (struct IntuitionBase *) 
  98.                    OpenLibrary("intuition.library", NULL);
  99.  
  100.    if (IntuitionBase == NULL) cleanup("Couldn't open Intuition\n", NULL);
  101. /*
  102. *  Allocate a Preferences structure
  103. */
  104.    prefs = (struct Preferences *) AllocMem(PSIZE, NULL);
  105.    if (prefs == NULL) cleanup("Out of memory\n", NULL);
  106. /*
  107. *  If commandline argument, find supplied file, otherwise find standard
  108. *  config file.
  109. */
  110.    if (argc <= 1) conffil = sysconf;
  111.    else           conffil = argv[1];
  112.  
  113.    fp = fopen(conffil,"r");
  114.    if (fp == NULL) cleanup("Couldn't find %s\n", conffil);
  115. /*
  116. *  Make sure it is a config file by checking its length
  117. */
  118.    length = dos_lenfil(conffil);
  119.       
  120.    if (length != PSIZE) cleanup("%s is not a configuration file??\n", conffil);
  121. /*
  122. *  Read it in.
  123. */
  124.    nread = fread(prefs, 1, (int) PSIZE, fp);
  125. /*
  126. *  If error during read, this is the last chance to get out.
  127. */
  128.    if (nread != (int) PSIZE) cleanup("Error reading %s\n", conffil);
  129. /*
  130. *  Okay, set the new preferences.
  131. */      
  132.    SetPrefs(prefs, PSIZE, 1L);
  133. /*
  134. *  Cleanup
  135. */
  136.    cleanup(NULL, NULL);
  137. }
  138.  
  139. /**
  140. *
  141. *   Clean up.
  142. *
  143. **/
  144. cleanup(string1, string2)
  145. char *string1, *string2;
  146. {
  147.    if (string1 && string2) printf(string1, string2);
  148.    else if (string1)       printf(string1);
  149.  
  150.    if (prefs) FreeMem(prefs, PSIZE);
  151.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  152.    if (fp) fclose(fp);
  153.  
  154.    exit(0);
  155. }
  156.  
  157.  
  158. long dos_lenfil(filnam)
  159. /**
  160. *
  161. *   Routine to get the length of file 'filnam'
  162. *
  163. **/
  164. char filnam[];
  165. {
  166.    struct FileInfoBlock *fileinfo;
  167.    struct Lock *lunin_lock;
  168.    long size;
  169. /*
  170. *  Reserve space for the file info block
  171. */
  172.    fileinfo = (struct FileInfoBlock *) 
  173.               AllocMem((long) sizeof(struct FileInfoBlock),0L);
  174.  
  175.    lunin_lock = Lock(filnam,ACCESS_READ);
  176.    Examine(lunin_lock,fileinfo);
  177.  
  178.    size = fileinfo->fib_Size;
  179.  
  180.    UnLock(lunin_lock);
  181.  
  182.    FreeMem(fileinfo,(long) sizeof(struct FileInfoBlock) );
  183.  
  184.    return(size);
  185. }
  186.  
  187.